<template>
  <div class="pagination">
    <div class="prev">
      <img class="active" :src="icon.prevActive" alt="" v-if="canPrev" @click="toPrev">
      <img :src="icon.prev" alt="" v-else>
    </div>
    <div class="next">
      <img class="active" :src="icon.nextActive" alt="" v-if="canNext" @click="toNext">
      <img :src="icon.next" alt="" v-else>
    </div>
  </div>
</template>

<script>
import prev from '../assets/prev.png';
import prevActive from '../assets/prev_active.png';
import next from '../assets/next.png';
import nextActive from '../assets/next_active.png';

export default {
  name: 'pagination',
  props: {
    pageSize: {
      type: Number,
    },
    totalSize: {
      type: Number,
    },
    initPage: {
      type: Number,
      default: 1,
    },
  },
  data() {
    return {
      icon: {
        prev,
        prevActive,
        next,
        nextActive,
      },
      currentPage: this.initPage,
    };
  },
  watch: {
    initPage(val) {
      this.currentPage = val;
    },
  },
  computed: {
    totalPage() {
      return Math.ceil(this.totalSize / this.pageSize);
    },
    canPrev() {
      return this.currentPage > 1;
    },
    canNext() {
      return this.currentPage < this.totalPage;
    },
  },
  methods: {
    toPrev() {
      if (this.canPrev) {
        this.currentPage = this.currentPage - 1;
        this.$emit('change', 'prev', this.currentPage);
      }
    },
    toNext() {
      if (this.canNext) {
        this.currentPage = this.currentPage + 1;
        this.$emit('change', 'next', this.currentPage);
      }
    },
  },
};
</script>

<style lang="scss">
.pagination {
  position: absolute;
  left: 0;
  top: 0;
  width: 100%;
  height: 40px;

  .active {
    cursor: pointer;
  }

  .prev {
    position: absolute;
    left: 20px;
    height: 40px;
  }

  .next {
    position: absolute;
    right: 20px;
    height: 40px;
  }
}
</style>
